<?php
// +----------------------------------------------------------------------
// | Fanwe 方维订餐小秘书商业系统
// +----------------------------------------------------------------------
// | Copyright (c) 2011 http://www.fanwe.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: 云淡风轻(88522820@qq.com)
// +----------------------------------------------------------------------


class ctmenuModule extends BizBaseModule
{
	public function __construct()
	{
		parent::__construct();
		$this->check_auth();
	}
	public function index()
	{				
		require_once APP_ROOT_PATH."app/Lib/page.php";
		$s_account_info = es_session::get("account_info");
		$account_id = intval($s_account_info['id']);
		$keyword = htmlspecialchars(addslashes(trim($_REQUEST['keyword'])));
		$sid = intval($_REQUEST['sid']);
		if($_REQUEST['is_redirect']==1)
		{
			$url_param=array(
				"keyword"=>$keyword,
				"sid"=>$sid
				);
			app_redirect(url("biz","ctmenu",$url_param));
		}
		
		$GLOBALS['tmpl']->assign("page_title","菜单列表");
		
		$supplier_id = intval($GLOBALS['db']->getOne("select supplier_id from ".DB_PREFIX."supplier_account where id = ".intval($s_account_info['id'])));
		$GLOBALS['tmpl']->assign("supplier_id",$supplier_id);
		
		$loactions = $GLOBALS['db']->getAll("SELECT id,name FROM ".DB_PREFIX."supplier_location WHERE deal_type =0 AND  id in(".implode(",",$s_account_info['location_ids']).") ");		
		$GLOBALS['tmpl']->assign("loactions",$loactions);
		
		$pageArgs = array();
		$ext_Where = " sl.id in  (".implode(",",$s_account_info['location_ids']).") ";
		if(!empty($_REQUEST['keyword'])){
			$pageArgs['keyword'] = $_REQUEST['keyword'];
			$ext_Where .= " AND slm.name like '%".$_REQUEST['keyword']."%'";
			$GLOBALS['tmpl']->assign("keyword",$_REQUEST['keyword']);
		}
		
		if(intval($_REQUEST['sid']) > 0){
			$pageArgs['sid'] = $_REQUEST['sid'];
			$ext_Where .= " AND slm.supplier_location_id  = '".intval($_REQUEST['sid'])."'";
			$GLOBALS['tmpl']->assign("sid",$_REQUEST['sid']);
		}
		
		
		$page = intval($_REQUEST['p']);
		if($page==0)
		$page = 1;
		$limit = (($page-1)*app_conf("PAGE_SIZE")).",".app_conf("PAGE_SIZE");
		
		$list = $GLOBALS['db']->getAll("SELECT slm.*,sl.name as location_name FROM ".DB_PREFIX."supplier_location_menu slm LEFT JOIN ".DB_PREFIX."supplier_location sl ON sl.id =slm.supplier_location_id WHERE ".$ext_Where." ORDER BY slm.id desc LIMIT ".$limit);
		$GLOBALS['tmpl']->assign("menu_list",$list);
		$count = $GLOBALS['db']->getOne("SELECT count(DISTINCT slm.id) FROM ".DB_PREFIX."supplier_location_menu slm LEFT JOIN ".DB_PREFIX."supplier_location sl ON sl.id =slm.supplier_location_id WHERE ".$ext_Where);
		
		$url_parameter = http_build_query($pageArgs);
		$page = new Page($count,app_conf("PAGE_SIZE"),"&".$url_parameter);   //初始化分页对象 		
		$p  =  $page->show();
		$GLOBALS['tmpl']->assign('pages',$p);
		
		$GLOBALS['tmpl']->display("biz/biz_ct_menu.html");
	}
	
	public function view()
	{
		$s_account_info = es_session::get("account_info");
		$account_id = intval($s_account_info['id']);
			
		$GLOBALS['tmpl']->assign("page_title","查看菜单");
		
		$supplier_id = intval($GLOBALS['db']->getOne("select supplier_id from ".DB_PREFIX."supplier_account where id = ".intval($s_account_info['id'])));
		$GLOBALS['tmpl']->assign("supplier_id",$supplier_id);
		
		$menu_id = intval($_REQUEST['id']);
		$menu_info = $GLOBALS['db']->getRow("select * from ".DB_PREFIX."supplier_location_menu where id = ".$menu_id." AND supplier_location_id in  (".implode(",",$s_account_info['location_ids']).") ");
	
		if($menu_info)
		{
			$menu_info['location_name'] = $GLOBALS['db']->getOne("SELECT `name` FROM ".DB_PREFIX."supplier_location WHERE id = " .$menu_info['supplier_location_id']);
			
			$menu_info['cate_name'] = $GLOBALS['db']->getOne("SELECT `name` FROM ".DB_PREFIX."deal_cate WHERE id = " .$menu_info['cate_id']);
			
			$GLOBALS['tmpl']->assign("menu_info",$menu_info);
			
			$sub_cate_list = $GLOBALS['db']->getAll("select t.* from ".DB_PREFIX."deal_cate_type as t left join ".DB_PREFIX."deal_cate_type_menu_link as l on l.deal_cate_type_id = t.id where l.menu_id = ".$menu_id." order by t.sort desc");
			$GLOBALS['tmpl']->assign("sub_cate_list",$sub_cate_list);
			
			$GLOBALS['tmpl']->display("biz/biz_ctmenu_view.html");
		}
		else
		{
			showErr($GLOBALS['lang']['SUPPLIER_NOT_MATCH'],0);
		}
	}
	
	
	public function publish(){
		$s_account_info = es_session::get("account_info");
		$account_id = intval($s_account_info['id']);
			
		$GLOBALS['tmpl']->assign("page_title","添加菜单");
		
		$supplier_id = intval($GLOBALS['db']->getOne("select supplier_id from ".DB_PREFIX."supplier_account where id = ".intval($s_account_info['id'])));
		$GLOBALS['tmpl']->assign("supplier_id",$supplier_id);
		
		$loactions = $GLOBALS['db']->getAll("SELECT id,name FROM ".DB_PREFIX."supplier_location WHERE id in(".implode(",",$s_account_info['location_ids']).") ");		
		$GLOBALS['tmpl']->assign("loactions",$loactions);
		
		$cate_list = $GLOBALS['db']->getAll("select * from ".DB_PREFIX."deal_cate where is_effect = 1 and is_delete =0 order by sort desc");
		$GLOBALS['tmpl']->assign("cate_list",$cate_list);
		
		$GLOBALS['tmpl']->display("biz/biz_ctmenu_publish.html");
	}
	
	public function insert(){
		$s_account_info = es_session::get("account_info");
		$account_id = intval($s_account_info['id']);
			
		$GLOBALS['tmpl']->assign("page_title",$GLOBALS['lang']['SUPPLIER_ORDER_VIEW']);
		
		$supplier_id = intval($GLOBALS['db']->getOne("select supplier_id from ".DB_PREFIX."supplier_account where id = ".intval($s_account_info['id'])));
		$GLOBALS['tmpl']->assign("supplier_id",$supplier_id);
		
		$data['name'] = htmlspecialchars(addslashes(trim($_REQUEST['name'])));
		if($data['name']==""){
			showErr("请输入菜单名称!");
			exit();
		}
		$data['supplier_location_id'] = intval($_REQUEST['supplier_location_id']);
		$data['preview'] = replace_public(addslashes(htmlspecialchars(trim($_REQUEST['preview']))));
		$data['price'] =  trim($_REQUEST['price']);
		$data['cate_id'] =  intval($_REQUEST['cate_id']);
		$data['is_special'] = intval($_REQUEST['is_special']);
		$data['brief'] = htmlspecialchars(addslashes(trim($_REQUEST['brief'])));
		$menu_id = $GLOBALS['db']->autoExecute(DB_PREFIX."supplier_location_menu",$data,"INSERT");
		$menu_id = $GLOBALS['db']->insert_id();
		if($menu_id > 0){
			$supplier_l_info['id'] = $data['supplier_location_id'];
			syn_supplier_locationcount($supplier_l_info);
			foreach($_REQUEST['deal_cate_type_id'] as $deal_cate_type_id)
			{
				if($deal_cate_type_id>0)
				{
					$menu_cate_type_link = array("menu_id"=>$menu_id,"deal_cate_type_id"=>intval($deal_cate_type_id));
					$GLOBALS['db']->autoExecute(DB_PREFIX."deal_cate_type_menu_link",$menu_cate_type_link);
				}
			}
			app_redirect(url("biz","ctmenu#view",array("id"=>$menu_id)));
		}
		else{
			showErr("添加菜单失败!",0);
		}
	}
	
	public function modify(){
		$s_account_info = es_session::get("account_info");
		$account_id = intval($s_account_info['id']);
			
		$GLOBALS['tmpl']->assign("page_title",$GLOBALS['lang']['SUPPLIER_ORDER_VIEW']);
		
		$supplier_id = intval($GLOBALS['db']->getOne("select supplier_id from ".DB_PREFIX."supplier_account where id = ".intval($s_account_info['id'])));
		$GLOBALS['tmpl']->assign("supplier_id",$supplier_id);
		
		$menu_id = intval($_REQUEST['id']);
		$menu_info = $GLOBALS['db']->getRow("select * from ".DB_PREFIX."supplier_location_menu where id = ".$menu_id." AND supplier_location_id in  (".implode(",",$s_account_info['location_ids']).") ");
	
		if($menu_info)
		{
			$cate_list = $GLOBALS['db']->getAll("select * from ".DB_PREFIX."deal_cate where is_effect = 1 and is_delete =0 order by sort desc");
			$GLOBALS['tmpl']->assign("cate_list",$cate_list);
			
			$loactions = $GLOBALS['db']->getAll("SELECT id,name FROM ".DB_PREFIX."supplier_location WHERE deal_type =0 AND id in(".implode(",",$s_account_info['location_ids']).") ");		
			$GLOBALS['tmpl']->assign("loactions",$loactions);
			$GLOBALS['tmpl']->assign("menu_info",$menu_info);
			$GLOBALS['tmpl']->display("biz/biz_ctmenu_modify.html");
		}
		else
		{
			showErr($GLOBALS['lang']['SUPPLIER_NOT_MATCH'],0);
		}
	}
	
	public function update(){
		$s_account_info = es_session::get("account_info");
		$account_id = intval($s_account_info['id']);
			
		$GLOBALS['tmpl']->assign("page_title",$GLOBALS['lang']['SUPPLIER_ORDER_VIEW']);
		
		$supplier_id = intval($GLOBALS['db']->getOne("select supplier_id from ".DB_PREFIX."supplier_account where id = ".intval($s_account_info['id'])));
		$GLOBALS['tmpl']->assign("supplier_id",$supplier_id);
		
		$menu_id = intval($_REQUEST['menu_id']);
		$menu_info = $GLOBALS['db']->getRow("select * from ".DB_PREFIX."supplier_location_menu where id = ".$menu_id." AND supplier_location_id in  (".implode(",",$s_account_info['location_ids']).") ");
	
		if($menu_info)
		{
			$data['name'] = htmlspecialchars(addslashes(trim($_REQUEST['name'])));
			if($data['name']==""){
				showErr("请输入菜单名称!");
				exit();
			}
			$data['supplier_location_id'] = intval($_REQUEST['supplier_location_id']);
			$data['preview'] = replace_public(addslashes(htmlspecialchars(trim($_REQUEST['preview']))));
			$data['price'] =  trim($_REQUEST['price']);
			$data['cate_id'] =  intval($_REQUEST['cate_id']);
			$data['is_special'] = intval($_REQUEST['is_special']);
 			$data['brief'] = htmlspecialchars(addslashes(trim($_REQUEST['brief'])));
 			if($GLOBALS['db']->autoExecute(DB_PREFIX."supplier_location_menu",$data,"UPDATE","id=".$menu_id)){
 				$supplier_l_info['id'] = $data['supplier_location_id'];
				syn_supplier_locationcount($supplier_l_info);
 				$GLOBALS['db']->query("delete from ".DB_PREFIX."deal_cate_type_menu_link where menu_id = ".$menu_id);
				foreach($_REQUEST['deal_cate_type_id'] as $deal_cate_type_id)
				{
					if($deal_cate_type_id>0)
					{
						$menu_cate_type_link = array("menu_id"=>$menu_id,"deal_cate_type_id"=>intval($deal_cate_type_id));
						$GLOBALS['db']->autoExecute(DB_PREFIX."deal_cate_type_menu_link",$menu_cate_type_link);
					}
				}
 				app_redirect(url("biz","ctmenu#view",array("id"=>$menu_id)));
 			}
 			else{
 				showErr("编辑菜单失败!",0);
 			}
		}
		else
		{
			showErr($GLOBALS['lang']['SUPPLIER_NOT_MATCH'],0);
		}
	}
	
	public function del(){
		$s_account_info = es_session::get("account_info");
		$account_id = intval($s_account_info['id']);
			
		$GLOBALS['tmpl']->assign("page_title",$GLOBALS['lang']['SUPPLIER_ORDER_VIEW']);
		
		$supplier_id = intval($GLOBALS['db']->getOne("select supplier_id from ".DB_PREFIX."supplier_account where id = ".intval($s_account_info['id'])));
		$GLOBALS['tmpl']->assign("supplier_id",$supplier_id);
		
		$menu_id = intval($_REQUEST['id']);
		$menu_info = $GLOBALS['db']->getRow("select * from ".DB_PREFIX."supplier_location_menu where id = ".$menu_id." AND supplier_location_id in  (".implode(",",$s_account_info['location_ids']).") ");
	
		if($menu_info)
		{
			if($GLOBALS['db']->query("DELETE FROM ".DB_PREFIX."supplier_location_menu WHERE id=".$menu_id)){
				$GLOBALS['db']->query("delete from ".DB_PREFIX."deal_cate_type_menu_link where menu_id = ".$menu_id);
				app_redirect(url("biz","ctmenu"));
			}
 			else{
 				showErr("删除菜单失败!",0);
 			}
		}
		else
		{
			showErr($GLOBALS['lang']['SUPPLIER_NOT_MATCH'],0);
		}
	}
	
	public function load_menu_cate_type()
	{
		$cate_id = intval($_REQUEST['cate_id']);
		$menu_id = intval($_REQUEST['menu_id']);

		
		$list = $GLOBALS['db']->getAll("select t.* from ".DB_PREFIX."deal_cate_type as t left join ".DB_PREFIX."deal_cate_type_link as l on l.deal_cate_type_id = t.id where l.cate_id = ".$cate_id." order by t.sort desc");
		if($list)
		{
			foreach($list as $k=>$v)
			{
				if($menu_id>0)
					$list[$k]['checked'] = $GLOBALS['db']->getOne("select count(*) from ".DB_PREFIX."deal_cate_type_menu_link where deal_cate_type_id = ".$v['id']." and menu_id = ".$menu_id);

			}
			$GLOBALS['tmpl']->assign("list",$list);
			$result['html'] = $GLOBALS['tmpl']->fetch("biz/biz_ctmenu_load_menu_cate_type.html");
			$result['status'] = 1;
		}
		else
		$result['status'] = 0;
		ajax_return($result);
	}
}

?>